#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>

#include "TFile.h"
#include "TString.h"
#include "TTree.h"

#include "CommandLineInterface.hh"
#include "Peaks.hh"

using namespace std;

int main(int argc, char* argv[]){
  char* InputFile = NULL;
  char* OutputFile = NULL;
  char* HOutputFile = NULL;
  char* Name = NULL;
  int Alpha = 0;
  bool Simulation = false;
  bool Test = false;
  int new2009 =0;
  CommandLineInterface* interface = new CommandLineInterface();

  interface->Add("-i", "inputfile", &InputFile);
  interface->Add("-o", "outputfile", &OutputFile);
  interface->Add("-h", "also produce histogramm output file _hist.root", &HOutputFile);
  interface->Add("-as", "0 not alpha source find zeros, \n                      1 alpha forward, \n                      2 alpha backward, \n                      3 cd calibration, \n                      4 alpha backward no foil, \n                      5 Pad calibration with Ne data", &Alpha);
  interface->Add("-s", "analyze simulated data", &Simulation);
  interface->Add("-t", "test", &Test);
  interface->Add("-n", "new 2009 data", &new2009);
  interface->CheckFlags(argc, argv);
  
  if(InputFile == NULL || OutputFile == NULL ){
    cerr<<"You have to provide both the input file and the output file!"<<endl;
    exit(1);
  }
  cout<<"input file: "<<InputFile<< endl;
  if(HOutputFile == NULL){
    cout<<"No Histogram file created."<<endl;
  }
  int npeaks = 1;
  bool rej = false;
  if(Alpha == 1) 
    npeaks = 3;
  else if(Alpha == 2)
    npeaks = 5;
  else if(Alpha == 4)
    npeaks = 4;

  if(new2009==1 && Alpha==2)
    npeaks = 4;

  if(Simulation)
    cout << "simulation " << endl;
  else
    cout << "data " << endl;
  

  TFile* File = new TFile(InputFile);
  TIter next(File->GetListOfKeys());
  TKey *key;
  ofstream output(OutputFile);
  output << "name\t";
  for(int j=0;j<npeaks;j++)
    output << "\tcont"<<j<<"\tmean"<<j<<"\tfwhm"<<j;
  output << "\tchi" << endl;
  int bd, strip, i, adc, ch, quadr;
  int rangemin=0;
  int rangemax=0;
  int res = 1;
  while ((key=(TKey*)next())){
    if(strcmp(key->GetClassName(),"TH1F") == 0){
      Name = (char*)key->GetName();
      //TEST
      if((strstr(Name,"ForStrip")) && (Test)){

	bd = 0; 
	strip = 0;
	i = 0;


	char temp[10], *ptr;
	ptr = Name;       //Hilfspointer
	while(*ptr < '0' || *ptr > '9') 
	  ptr++; // Bis zur ersten Zahl
	while(*ptr >= '0' && *ptr <= '9') //Alle aneinanderh?ngenden Zahlen
	  temp[i++] = *ptr++;  // in temp speichern
	temp[i] = '\0'; //0-Terminierung 
	bd = atoi(temp); //in int umwandeln
    
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	strip = atoi(temp);
	//cout << "BD " << bd << " Strip " << strip << endl;

	if((bd==4) && (strip==4)){
	  rangemin=1500;
	  rangemax=4000;
	  npeaks = 6;
	  res = 30;
	  rej =true;
	}
	else
	  continue;
	
	if(HOutputFile)
	  Peaks(File, Name, OutputFile, rangemin, rangemax, npeaks, res, 0.05, rej,HOutputFile);
      }
      //END TEST
      //padcal
      if((strstr(Name,"Erestcal")) && (Alpha==5) && (!Simulation) && (!Test)){
	bd = 0; 
	strip = 0;
	int delta =0;
	i = 0;


	char temp[10], *ptr;
	ptr = Name;       //Hilfspointer
	while(*ptr < '0' || *ptr > '9') 
	  ptr++; // Bis zur ersten Zahl
	while(*ptr >= '0' && *ptr <= '9') //Alle aneinanderh?ngenden Zahlen
	  temp[i++] = *ptr++;  // in temp speichern
	temp[i] = '\0'; //0-Terminierung 
	bd = atoi(temp); //in int umwandeln
    
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	strip = atoi(temp);
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	delta = atoi(temp);
	//cout << "BD " << bd << " Strip " << strip << " delta " << delta << endl;
	
	rangemin=6;
	rangemax=200;
	npeaks = 2;
	res = 5;
	rej = true;

	if(bd==3)
	 continue;
	if(strip>13)
	  continue;
	if(delta<3)
	  continue;
	if(delta>7)
	  npeaks=1;
	//if(strip!=6)
	//  continue;
	if(strip<4)
	  npeaks=1;
	if(HOutputFile)
	  Peaks(File, Name, OutputFile, rangemin, rangemax, npeaks, res, 0.05, rej,HOutputFile);
      } //end padcal
      //ZEROS
      if((strstr(Name,"E_part")) && (Alpha==0) && (!Simulation) && (!Test)){
	rangemin=0;
	rangemax=300;
	npeaks = 1;
	res = 30;
	rej = true;

	adc = 0; 
	ch = 0;
	i = 0;


	char temp[10], *ptr;
	ptr = Name;       //Hilfspointer
	while(*ptr < '0' || *ptr > '9') 
	  ptr++; // Bis zur ersten Zahl
	while(*ptr >= '0' && *ptr <= '9') //Alle aneinanderh?ngenden Zahlen
	  temp[i++] = *ptr++;  // in temp speichern
	temp[i] = '\0'; //0-Terminierung 
	adc = atoi(temp); //in int umwandeln
    
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	ch = atoi(temp);
	//cout << "BD " << bd << " Strip " << strip << endl;

	if((adc==2) || (adc==5)){
	  if((ch%2==1) && (ch<8)){
	    cout << " adc " << adc << " ch " << ch << "E detector" << endl;
	    res = 15;
	  }
	}
	
	if(HOutputFile)
	  Peaks(File, Name, OutputFile, rangemin, rangemax, npeaks, res, 0.5, rej,HOutputFile);
	//else
	//Peaks(File, Name, OutputFile, 0, 300, npeaks, 1, 0.5,rej);
      }
      //END ZEROS
      //ALPHA
      if( (strstr(Name,"ForStrip")) && (Alpha>0)  && (Alpha<3)  && (!Simulation) && (!Test)){

	bd = 0; 
	strip = 0;
	i = 0;


	char temp[10], *ptr;
	ptr = Name;       //Hilfspointer
	while(*ptr < '0' || *ptr > '9') 
	  ptr++; // Bis zur ersten Zahl
	while(*ptr >= '0' && *ptr <= '9') //Alle aneinanderh?ngenden Zahlen
	  temp[i++] = *ptr++;  // in temp speichern
	temp[i] = '\0'; //0-Terminierung 
	bd = atoi(temp); //in int umwandeln
    
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	strip = atoi(temp);
	//cout << "BD " << bd << " Strip " << strip << endl;
	if(Alpha == 1 && (strstr(Name,"Forward"))){ // bd <4 forward
	  res = 1;
	  if( bd >3 )
	    continue;
	  if( strip>4 )
	    continue;
	  else if( (bd == 0) && (strip <5)){
	    rangemin=165;
	    rangemax=230;
	    res = 3;
	    rej =true;
	  }
	  else if( (bd == 1) && (strip <5)){
	    rangemin=180;
	    rangemax=250;
	    res = 3;
	    rej =true;
	  }
	  else if( (bd == 2) && (strip <5)){
	    rangemin=180;
	    rangemax=270;
	    res = 3;
	    rej =true;
	  }
	  else if( (bd == 3) && (strip <4)){	
	    rangemin=140;
	    rangemax=220;
	    res = 3;
	    rej =true;
	  }
	  else if( (bd == 3) && (strip ==4)){	
	    rangemin=130;
	    rangemax=220;
	    res = 3;
	    rej =true;
	  }
	  if(new2009==1){
	    rangemin=50;
	    rangemax=200;
	    res = 3;
	    rej =true;
	  }
	} // bd <4 forward
	else{ // backward
	  if( bd < 4)
	    continue;
	  else if((strip>3) && (strip <9)){
	    rangemin=1500;
	    rangemax=4000;
	    npeaks = 6;
	    res = 30;
	    rej =true;
	  }
	  else
	    continue;
	}
	if(HOutputFile)
	  Peaks(File, Name, OutputFile, rangemin, rangemax, npeaks, res, 0.05, rej,HOutputFile);
      }
      //END ALPHA

      //ALPHA NO FOIL BW
      if( (strstr(Name,"dEForStrip")) && (strstr(Name,"Backward")) && (Alpha==4) ){
	bd = 0; 
	strip = 0;
	i = 0;


	char temp[10], *ptr;
	ptr = Name;       //Hilfspointer
	while(*ptr < '0' || *ptr > '9') 
	  ptr++; // Bis zur ersten Zahl
	while(*ptr >= '0' && *ptr <= '9') //Alle aneinanderh?ngenden Zahlen
	  temp[i++] = *ptr++;  // in temp speichern
	temp[i] = '\0'; //0-Terminierung 
	bd = atoi(temp); //in int umwandeln
    
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	strip = atoi(temp);
	cout << "BD " << bd << " Strip " << strip << endl;
	rangemin=1500;
	rangemax=4000;
	if(new2009){
	  rangemin=200;
	  rangemax=1000;	  
	}
	npeaks = 4;
	res = 10;
	rej =true;
	if(HOutputFile)
	  Peaks(File, Name, OutputFile, rangemin, rangemax, npeaks, res, 0.05, rej,HOutputFile);
      }
      //END ALPHA NO FOIL BW


      //CD ALPHA     
      if( (strstr(Name,"BackwardCD_dEfor")) && (Alpha==3)  && (!Simulation) && (!Test)){

	if(new2009==1 && (strstr(Name,"BackwardCD_dEforRing")))
	  continue;
	bd = 0; 
	strip = 0;
	i = 0;


	char temp[10], *ptr;
	ptr = Name;       //Hilfspointer
	while(*ptr < '0' || *ptr > '9') 
	  ptr++; // Bis zur ersten Zahl
	while(*ptr >= '0' && *ptr <= '9') //Alle aneinanderh?ngenden Zahlen
	  temp[i++] = *ptr++;  // in temp speichern
	temp[i] = '\0'; //0-Terminierung 
	bd = atoi(temp); //in int umwandeln
    
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	strip = atoi(temp);
	cout << "data quadrant " << bd << " Strip " << strip << endl;

	rangemin=1000/2;
	rangemax=3500/2;
	npeaks = 5;
	res = 20;
	rej =false;
	if(new2009==1){
	  rangemin=50;
	  rangemax=250;
	  npeaks = 4;
	  res = 2;
	  rej =true;
	}
	if(HOutputFile)
	  Peaks(File, Name, OutputFile, rangemin, rangemax, npeaks, res, 0.02, rej,HOutputFile);
      }
      //END CD ALPHA

      //SIMULATION     
      if( (strstr(Name,"dEForStrip")) &&(Simulation) && (!Test) && (Alpha!=3)){
	strip = 0;
	i = 0;
	rej =false;

	char temp[10], *ptr;
	ptr = Name;       //Hilfspointer
	while(*ptr < '0' || *ptr > '9') 
	  ptr++; // Bis zur ersten Zahl
	while(*ptr >= '0' && *ptr <= '9') //Alle aneinanderh?ngenden Zahlen
	  temp[i++] = *ptr++;  // in temp speichern
	temp[i] = '\0'; //0-Terminierung 
	bd = atoi(temp); //in int umwandeln
    
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	strip = atoi(temp);
	//cout << "BD " << bd << " Strip " << strip << endl;
	//cout << "name " << Name << endl;
	if( strstr(Name,"ForwardBarrel")  && (Alpha==1)){
	  if( strip>3 )
	    continue;
	  else{
	    npeaks = 3;
	    rangemin=3000/5;
	    rangemax=4500/5;
	    res = 30;
	    rej =true;
	  }
	}
	else if( strstr(Name,"BackwardBarrel")  && (Alpha==2)){
	  bd = 1; 
	  rangemin=2000;
	  rangemax=6000;
	  res = 30;
	  rej =true;
	  if( strip>11 )
	    npeaks = 4;
	  else
	    npeaks = 6;	  
	}
	else
	  continue;
	if(HOutputFile){
	  cout << "min " << rangemin << " max " << rangemax << " npeaks " << npeaks << endl;
	  Peaks(File, Name, OutputFile, rangemin, rangemax, npeaks, res, 0.05, rej,HOutputFile);

	}
      } //END SIMULATION   

        //SIMULATION CD  
      if( (strstr(Name,"BackwardCD")) &&(Simulation) && (!Test) && (Alpha==3)){

	bd = 0; 
	strip = 0;
	i = 0;

	rej =true;

	char temp[10], *ptr;
	ptr = Name;       //Hilfspointer
	while(*ptr < '0' || *ptr > '9') 
	  ptr++; // Bis zur ersten Zahl
	while(*ptr >= '0' && *ptr <= '9') //Alle aneinanderh?ngenden Zahlen
	  temp[i++] = *ptr++;  // in temp speichern
	temp[i] = '\0'; //0-Terminierung 
	bd = atoi(temp); //in int umwandeln
    
	while(*ptr < '0' || *ptr > '9')
	  ptr++;
	i=0;
	while(*ptr >= '0' && *ptr <= '9' && *ptr)
	  temp[i++] = *ptr++;
	temp[i] = '\0';
	strip = atoi(temp);
	cout << "simation quadrant " << bd << " Strip " << strip << endl;

	npeaks = 6;
	rangemin=0;
	rangemax=7000;
	res = 20;
	if(HOutputFile)
	  Peaks(File, Name, OutputFile, rangemin, rangemax, npeaks, res, 0.05, rej,HOutputFile);
      } //END SIMULATION  CD
      
    }
  }
  return 0;
}
